package com.wrx.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wrx.server.mapper.BarMapper;
import com.wrx.server.mapper.CollectMapper;
import com.wrx.server.mapper.ThemePostMapper;
import com.wrx.server.pojo.Collect;
import com.wrx.server.pojo.CollectBar;
import com.wrx.server.pojo.RespBean;
import com.wrx.server.service.ICollectService;
import com.wrx.server.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wrx
 * @since 2021-04-20
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements ICollectService {

    @Autowired
    IUserService userService;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    BarMapper barMapper;
    @Autowired
    ThemePostMapper themePostMapper;

    @Override
    public RespBean addCollectThemePost(Collect collect,HttpServletRequest request) {
        if (collect.getTheme_post_id()==null || collect.getTheme_post_id()<1){
            return RespBean.error("帖子id异常，收藏失败请重试");
        }

        Integer userId = userService.getUserInfo(request.getUserPrincipal()).getUserId();
        if (userId == null){
            return RespBean.error("收藏失败，请先登录！");
        }
        collect.setUser_id(userId);

        if (themePostMapper.selThePostIdByThemePostId(collect.getTheme_post_id())==null) {
            return RespBean.error("收藏失败，主题帖id异常，请重试");
        }

        Integer collectId = collectMapper.selCollectId(collect);
        if (collectId!=null){
            return RespBean.error("收藏记录已存在！");
        }

        collectMapper.addCollectThemePost(collect);
        if (collect.getCollect_id()==null){
            return  RespBean.error("收藏失败，请重试！");
        }
        return RespBean.success("收藏成功！");
    }

    @Override
    public RespBean delCollectThemePost(Collect collect, HttpServletRequest request) {
        if (collect.getTheme_post_id()==null || collect.getTheme_post_id()<1){
            return RespBean.error("帖子id异常，取消收藏失败请重试");
        }

        Integer userId = userService.getUserInfo(request.getUserPrincipal()).getUserId();
        if (userId == null){
            return RespBean.error("取消收藏失败，请先登录！");
        }
        collect.setUser_id(userId);

        if (themePostMapper.selThePostIdByThemePostId(collect.getTheme_post_id())==null) {
            return RespBean.error("取消收藏失败，主题帖id异常，请重试");
        }

        Integer collectId = collectMapper.selCollectId(collect);
        if (collectId==null){
            return RespBean.error("收藏记录不存在！");
        }

        Integer delState = collectMapper.delCollectThemePost(collect);

        if (delState==0){
            return  RespBean.error("取消收藏失败，收藏记录不存在！");
        }
        return RespBean.success("取消收藏成功！");

    }


    @Override
    public RespBean addCollectBar(CollectBar collectBar, HttpServletRequest request) {
        if (collectBar.getBar_id()==null || collectBar.getBar_id()<1){
            return RespBean.error("吧id异常，关注失败，请重试");
        }

        Integer userId = userService.getUserInfo(request.getUserPrincipal()).getUserId();
        if (userId == null){
            return RespBean.error("关注失败，请先登录！");
        }
        collectBar.setUser_id(userId);

        if (barMapper.selBarIdByBarId(collectBar.getBar_id())!=1){
            return RespBean.error("关注失败，贴吧id不存在");
        }

        Integer collectId = collectMapper.selCollectBarId(collectBar);
        if (collectId!=null){
            return RespBean.error("关注记录已存在！");
        }

        collectMapper.addCollectBar(collectBar);
        if (collectBar.getCollect_bar_id()==null){
            return  RespBean.error("关注失败，请重试！");
        }
        barMapper.addBarUserNumber(collectBar.getBar_id());
        return RespBean.success("关注成功！");
    }

    @Override
    public RespBean delCollectBar(CollectBar collectBar, HttpServletRequest request) {
        if (collectBar.getBar_id()==null || collectBar.getBar_id()<1){
            return RespBean.error("吧id异常，取消关注失败，请重试");
        }

        Integer userId = userService.getUserInfo(request.getUserPrincipal()).getUserId();
        if (userId == null){
            return RespBean.error("取消关注失败，请先登录！");
        }
        collectBar.setUser_id(userId);

        if (barMapper.selBarIdByBarId(collectBar.getBar_id())!=1){
            return RespBean.error("取消关注失败，贴吧id不存在");
        }

        Integer collectId = collectMapper.selCollectBarId(collectBar);
        if (collectId==null){
            return RespBean.error("取消关注失败，关注记录不存在！");
        }

        Integer delState = collectMapper.delCollectBar(collectBar);
        System.out.println(delState);
        if (delState==0){
            return  RespBean.error("取消关注失败，请重试！");
        }
        barMapper.delBarUserNumber(collectBar.getBar_id());
        return RespBean.success("取消关注成功！");
    }


}
